package com.Arsanesia.TempleRush.Views.Screens;

import com.Arsanesia.TempleRush.TempleRush;
import com.Arsanesia.TempleRush.Controllers.Listeners.PreferencesButtonListener;
import com.Arsanesia.TempleRush.Controllers.Listeners.SettingButtonListener;
import com.Arsanesia.TempleRush.Controllers.Listeners.ButtonListeners.*;
import com.Arsanesia.TempleRush.Enumeration.TempleRushMusic;
import com.Arsanesia.TempleRush.Models.GameConfiguration;
import com.Arsanesia.TempleRush.Views.Components.Buttons.MainMenuButton;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;


public class MenuScreen extends AbstractScreen{
	private Image bgImage;
	
	private float YPlayThreshold = 60;
	private float SIDE_MAINBUTTON_PADDING = 19;
	private float LEFT_MAINBUTTON_MARGIN = 3.75f;
	private float RIGHT_MAINBUTTON_MARGIN = 3.75f;
	private float BOTTOM_MAINBUTTON_MARGIN = 5.5f;	
	
	private MainMenuButton playbutton;
	private MainMenuButton settingbutton;
	private MainMenuButton highscorebutton;
	private MainMenuButton aboutbutton;
	private MainMenuButton helpbutton;
	private MainMenuButton quitbutton;
	private ImageButton soundbutton;
	private ImageButton vibratebutton;
	public MenuScreen(TempleRush game){
		super(game);
		
	}

	@Override
	public void show(){
		super.show();
		//load atlas resource for main menu
		atlas = new TextureAtlas(Gdx.files.internal("Images/MainMenu/mainmenu.atlas"));
		//set background image
		bgImage = new Image(atlas.findRegion("bg_mainmenu"));
		actualBgWidth = bgImage.getWidth();
		actualBgHeight = bgImage.getHeight();
		//immediately add background image to stage
		this.addActor(bgImage);
		
		float width = Gdx.graphics.getWidth();
		float height = Gdx.graphics.getHeight();
		
		//make the buttons
		//make playbutton
		playbutton = makeMainMenuButton("playmenu",width, height);
		//add play listener
		playbutton.addListener(new PlayButtonListener(game));
		
		//make subsetting buttons
		//create vibratebutton and make invisible
		Array<Sprite> pausevibratesprites = atlas.createSprites("vibrate");
		vibratebutton = new ImageButton(new TextureRegionDrawable(new TextureRegion(pausevibratesprites.get(0))),
				 new TextureRegionDrawable(new TextureRegion(pausevibratesprites.get(0))),
				 new TextureRegionDrawable(new TextureRegion(pausevibratesprites.get(1))));
		vibratebutton.addAction(Actions.alpha(0));
		vibratebutton.setChecked(!GameConfiguration.isVibrate());
		vibratebutton.addListener(new VibrationConfigurationListener());
		
		//create soundbutton and make invisible
		Array<Sprite> pausesoundsprites = atlas.createSprites("sound");
		soundbutton = new ImageButton(new TextureRegionDrawable(new TextureRegion(pausesoundsprites.get(0))),
				 new TextureRegionDrawable(new TextureRegion(pausesoundsprites.get(0))),
				 new TextureRegionDrawable(new TextureRegion(pausesoundsprites.get(1))));
		soundbutton.addAction(Actions.alpha(0));
		soundbutton.setChecked(!GameConfiguration.isSoundEnabled());
		//add sound listener
		soundbutton.addListener(new SoundConfigurationListener());
		
		//make setting button
		settingbutton = makeMainMenuButton("setting",width, height);
		settingbutton.addListener(new PreferencesButtonListener(game));//new SettingButtonListener(soundbutton, vibratebutton, settingbutton.getHeight()));
		
		//add highscore button
		highscorebutton = makeMainMenuButton("highscore",width, height);
		//add high score listener
		highscorebutton.addListener(new HighScoreButtonListener(game));
		
		//make about button
		aboutbutton = makeMainMenuButton("about",width, height);
		//add about listener
		aboutbutton.addListener(new AboutButtonListener(game));
		
		//make help button
		helpbutton = makeMainMenuButton("help",width, height);
		//add help listener
		helpbutton.addListener(new HelpButtonListener(game));
		
		//make quit button
		quitbutton = makeMainMenuButton("quit", width, height);
		quitbutton.addListener(new ExitButtonListener());
		//add all the buttons to stage
		this.addActor(playbutton);
		this.addActor(soundbutton);
		this.addActor(vibratebutton);
		this.addActor(settingbutton);
		this.addActor(highscorebutton);
		this.addActor(aboutbutton);
		//this.addActor(helpbutton);
		this.addActor(quitbutton);
		
		GameConfiguration.musicmanager.play(TempleRushMusic.MENU);
	}
	
	@Override
	public void resize(int width, int height){
		super.resize(width, height);
		
		//reposition all buttons
		playbutton.setPosition((bgImage.getWidth()-playbutton.getWidth())/2, ((bgImage.getHeight()-playbutton.getHeight())/2)-YPlayThreshold);
		
		settingbutton.setPosition(LEFT_MAINBUTTON_MARGIN, BOTTOM_MAINBUTTON_MARGIN);
		
		highscorebutton.setPosition(settingbutton.getX()+settingbutton.getWidth()+SIDE_MAINBUTTON_PADDING, settingbutton.getY());
		
		aboutbutton.setPosition(highscorebutton.getX()+highscorebutton.getWidth()+SIDE_MAINBUTTON_PADDING, highscorebutton.getY());
		
		quitbutton.setPosition(aboutbutton.getX() + aboutbutton.getWidth()+SIDE_MAINBUTTON_PADDING, aboutbutton.getY());
		
		helpbutton.setPosition(bgImage.getWidth()-RIGHT_MAINBUTTON_MARGIN - quitbutton.getWidth(), BOTTOM_MAINBUTTON_MARGIN + quitbutton.getHeight());
		
		

		
	}
	
	public MainMenuButton makeMainMenuButton(String imagenameonatlas, float width, float height){

		Array<Sprite> buttonsprites = atlas.createSprites(imagenameonatlas);	
		//make the drawable from sprites
		SpriteDrawable spriteDrawableUp = new SpriteDrawable(buttonsprites.get(MainMenuButton.UP_BUTTON));
		SpriteDrawable spriteDrawableDown = new SpriteDrawable(buttonsprites.get(MainMenuButton.DOWN_BUTTON));
		//create the mainmenu button
		MainMenuButton mainmenubutton = new MainMenuButton(spriteDrawableUp,spriteDrawableDown);
		return mainmenubutton;
	}
	

}
